package com.label.Web;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.label.Config.Exception.CustomException;
import com.label.Config.Exception.CustomUnauthorizedException;
import com.label.Config.Logger.LogAnnotation;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Common.Validation.Del;
import com.label.Model.Common.Validation.Save;
import com.label.Model.Common.Validation.Update;
import com.label.Model.Do.Permission;
import com.label.Model.Query.PermissionQuery;
import com.label.Service.PermissionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @GetMapping("/findAllPermission")
    @LogAnnotation(module = "权限模块",operator = "获取所有权限")
    @RequiresAuthentication
    public ResponseBean findAllPermission() {
        return new ResponseBean(HttpStatus.OK.value(), "查询成功！", JSONObject.toJSONString(permissionService.list()));
    }

    @PostMapping("/addPermission")
    @LogAnnotation(module = "权限模块",operator = "添加权限")
    @RequiresAuthentication
    public ResponseBean addPermission(@Validated(value = Save.class) @RequestBody PermissionQuery permissionQuery) {
        try {
            Permission perTemp = permissionService.getOne(new QueryWrapper<Permission>().lambda().eq(Permission::getName, permissionQuery.getName()).eq(Permission::getPerCode, permissionQuery.getPerCode()));
            if (perTemp != null) {
                throw new CustomUnauthorizedException("该权限已存在！");
            }
            boolean flag = permissionService.save(BeanUtil.copyProperties(permissionQuery, Permission.class));
            if (!flag) {
                throw new CustomException("新增失败！");
            }
            return new ResponseBean(HttpStatus.OK.value(), "新增成功！", true);
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "新增失败！", false);
        }
    }

    @PostMapping("/deletePermission")
    @LogAnnotation(module = "权限模块",operator = "删除权限")
    @RequiresAuthentication
    public ResponseBean deletePermission(@Validated(value = Del.class) @RequestBody PermissionQuery permissionQuery) {
        try {
            boolean flag = permissionService.removeById(permissionQuery.getId());
            if (!flag) {
                throw new CustomException("删除失败，权限ID不存在！");
            }
            return new ResponseBean(HttpStatus.OK.value(), "删除成功！", null);
        }catch (Exception e){
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败！", false);
        }
    }

    @PostMapping("/updatePermission")
    @LogAnnotation(module = "权限模块",operator = "更新权限")
    @RequiresAuthentication
    public ResponseBean updatePermission(@Validated(value = Update.class) @RequestBody PermissionQuery permissionQuery) {
            Permission perTemp1 = permissionService.getOne(new QueryWrapper<Permission>().lambda().eq(Permission::getId, permissionQuery.getId()));
            if(perTemp1 == null) {
                throw new CustomUnauthorizedException("该权限不存在！");
            }
            if (permissionQuery.getPerCode() != null) {
                Permission perTemp2 = permissionService.getOne(new QueryWrapper<Permission>().lambda().eq(Permission::getPerCode,permissionQuery.getPerCode()));
                if(perTemp2 != null) {
                    throw new CustomUnauthorizedException("该权限代码已存在！");
                }
            }
            boolean flag = permissionService.updateById(BeanUtil.copyProperties(permissionQuery, Permission.class));
            if (!flag) {
                throw new CustomException("更新失败！");
            }
            return new ResponseBean(HttpStatus.OK.value(), "更新成功！", true);
    }

}
